package cn.song.constants.enums;

import cn.song.constants.BaseEnum;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 性别枚举
 *
 * @author SongRenShuo
 * @date 2025/01/05
 */
@Getter
public enum GenderEnum implements BaseEnum {
    /**
     * 未知
     */
    UNKNOWN(0, "未知"),
    /**
     * 男
     */
    MAN(1, "男"),
    /**
     * 女
     */
    WOMAN(2, "女");

    /**
     * 性别编码
     * <p>
     * 标记数据库存的值是code
     */
    private final Integer code;
    /**
     * 性别名称
     * <p>
     * JsonValue 枚举序列化时返回名称
     */
    @JsonValue
    private final String name;


    GenderEnum(int code, String name) {
        this.code = code;
        this.name = name;
    }

    private static final HashMap<Integer, GenderEnum> GENDER_MAP = new HashMap<>(8);
    private static final HashMap<String, GenderEnum> GENDER_MAP_NAME = new HashMap<>(8);

    static {
        Arrays.stream(values()).forEach(genderEnum -> {
            GENDER_MAP.put(genderEnum.code, genderEnum);
            GENDER_MAP_NAME.put(genderEnum.name, genderEnum);
        });
    }

    /**
     * 通过code获取枚举
     *
     * @param code code
     * @return {@link GenderEnum }
     * @date 2025/01/05
     * @author SongRenShuo
     */
    public static GenderEnum valueOf(Integer code) {
        if (code == null) {
            return null;
        }

        return GENDER_MAP.get(code);
    }

    /**
     * 通过name获取枚举
     *
     * @param name 名称
     * @return {@link GenderEnum }
     * @date 2025/01/20
     * @author SongRenShuo
     */
    public static GenderEnum valueOfName(String name) {
        if (name == null || name.isEmpty()) {
            return null;
        }

        return GENDER_MAP_NAME.get(name);
    }


}
